using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;
using DTO;

namespace DAOLayer
{
    public class TacGiaDao
    {
        public static IList LayDanhSach()
        {
            ArrayList ds = new ArrayList();
            OleDbConnection cn;
            string strSQL;

            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            cn = DataProvider.ConnectionData();
            
            // B3: Tao chuoi strSQL thao tac CSDL
            strSQL = "Select * From Tacgia";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr;
            dr = cmd.ExecuteReader();

            //TacGiaDto tgia = new TacGiaDto();
            while (dr.Read())
            {
                TacGiaDto tgia = new TacGiaDto();
                tgia.MATACGIA = (int)dr["MATACGIA"];
                tgia.TENTACGIA = (string)dr["TENTACGIA"];
                ds.Add(tgia);
            }
            // B5: Dong ket noi CSDL
            dr.Close();
            cn.Close();

            return ds;
        }
        public DataTable DanhSach()
        {
            OleDbConnection cn = DataProvider.ConnectionData();
            string strSQL = "select * from Tacgia";

            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public static void Them(TacGiaDto tacgiaDto)
        {
            OleDbConnection cn;
            cn = DataProvider.ConnectionData();

            string strSQL;
            strSQL = "Insert into Tacgia(Tentacgia) values (?)" +
            "";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@Tentacgia", OleDbType.WChar);
            cmd.Parameters["@Tentacgia"].Value = tacgiaDto.TENTACGIA;

            cmd.ExecuteNonQuery();
            strSQL = "Select @@IDENTITY";
            cmd = new OleDbCommand(strSQL, cn);
            tacgiaDto.MATACGIA = (int)cmd.ExecuteScalar();

            cn.Close();
        }
    }
}
